import Vue from "vue";
import VueRouter from "vue-router";

// 解决重复点击路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    meta: {
      title: "登录",
    },
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    meta: {
      title: "404",
    },
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    meta: { title: "首页", icon: "el-icon-menu" },
    children: [
      {
        path: "dashboard",
        name: "首页",
        component: () => import("@/views/dashboard/index"),
        // meta: { title: "首页", icon: "el-icon-menu" },
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

export const asyncRoutes = [
  {
    path: "/test",
    component: Layout,
    redirect: "/test/test",
    meta: { title: "测试", icon: "el-icon-menu", roles: ["admin"] },
    children: [
      {
        path: "/test/test",
        name: "测试",
        component: () => import("@/views/test/index"),
        meta: { title: "测试", icon: "el-icon-menu", roles: ["admin"] },
      },
    ],
  },
];

const router = new VueRouter({
  routes: constantRoutes.concat(asyncRoutes),
});

export function resetRouter() {
  const newRouter = router;
  router.matcher = newRouter.matcher; // reset router
}

export default router;
